<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="生产月份" prop="scny">
        <el-date-picker
          :editable="false"
          clearable
          v-model="queryParams.scny"
          type="monthrange"
          range-separator="至"
          start-placeholder="开始月份"
          end-placeholder="结束月份"
          value-format="yyyy-MM"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="结束日期" prop="jsrq">
        <el-date-picker
         :editable="false"
          clearable
          size="small"
          v-model="queryParams.jsrq"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择结束日期"
        >
        </el-date-picker>
      </el-form-item> -->
      <!--  <el-form-item label="报表名称" prop="bbmc">
        <el-input
          v-model="queryParams.bbmc"
          placeholder="请输入报表名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报表编号" prop="bbbh">
        <el-input
          v-model="queryParams.bbbh"
          placeholder="请输入报表编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属基地" prop="jd">
        <el-input
          v-model="queryParams.jd"
          placeholder="请输入所属基地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="oneKey"
          >生成</el-button
        > -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['productionProcess:analysisProductMonth:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['productionProcess:analysisProductMonth:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['productionProcess:analysisProductMonth:remove']"
          >删除</el-button
        >
      </el-col>
      <!--  <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['productionProcess:analysisProductMonth:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="analysisProductMonthList"
      @selection-change="handleSelectionChange"
      max-height="500"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" width="100px" />
      <el-table-column label="生产月份" align="center" prop="scny" width="180">
      </el-table-column>
      <!--  <el-table-column label="结束日期" align="center" prop="jsrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jsrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="报表名称" align="center" prop="bbmc" />
      <el-table-column label="报表编号" align="center" prop="bbbh" />
      <el-table-column label="所属基地" align="center" prop="jd" />
      <!--  <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="250px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="excelhandleView(scope.row)"
            >导出</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['productionProcess:analysisProductMonth:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['productionProcess:analysisProductMonth:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仰口生产报每月对话框 -->
    <!-- <el-dialog fullscreen
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      v-myDialogDrag
    > -->
    <el-dialog
      fullscreen
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      custom-class="myDialog"
    >
      <TableHeader
        @nameChange="nameChange"
        :data="{
          mainTitle: '仰口生产月报表记录',
          company: '青岛崂山矿泉水有限公司',
          code: form.bbbh,
        }"
      />
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产月份" prop="scny">
              <el-date-picker
                :editable="false"
                clearable
                v-model="form.scny"
                type="month"
                value-format="yyyy-MM"
                placeholder="选择生产月份"
                :disabled="openDisabled"
                @change="correlation"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="所属基地" prop="jd">
              <el-select
                v-model="queryParams.factoryId"
                placeholder="请选择所属基地"
                clearable
                size="small"
              >
                <el-option
                  v-for="dict in factoryListOptions"
                  :key="dict.deptId"
                  :label="dict.name"
                  :value="dict.deptId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始日期" prop="ksrq">
              <el-date-picker
                :editable="false"
                clearable
                v-model="form.ksrq"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择开始日期"
                :disabled="openDisabled"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="jsrq">
              <el-date-picker
                :editable="false"
                clearable
                v-model="form.jsrq"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择结束日期"
                :disabled="openDisabled"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="报表名称" prop="bbmc">
              <el-input
                v-model="form.bbmc"
                placeholder="请输入报表名称"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报表编号" prop="bbbh">
              <el-input
                v-model="form.bbbh"
                placeholder="请输入报表编号"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col> -->

          <!--  <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col> -->
        </el-row>
        <br />
        <el-divider content-position="center">仰口生产月报表子信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <!-- <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddRptAnalysisProductMonthSub"
              :disabled="openDisabled"
              >添加</el-button
            >
          </el-col> -->
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="oneKey"
              :disabled="openDisabled"
              >生成</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteRptAnalysisProductMonthSub"
              :disabled="openDisabled"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="rptAnalysisProductMonthSubList"
          :row-class-name="rowRptAnalysisProductMonthSubIndex"
          @selection-change="handleRptAnalysisProductMonthSubSelectionChange"
          ref="rptAnalysisProductMonthSub"
          max-height="500"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <!-- <el-table-column label="品种规格" prop="gg">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.gg"
                placeholder="请输入品种规格"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column> -->
          <el-table-column label="品种规格" prop="ggname" min-width="320">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.ggname"
                placeholder="请输入品种规格"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="箱、包" prop="xb" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.xb"
                placeholder="请输入箱、包"
                :disabled="openDisabled"
                @change="updateAll(scope, $event)"
              />
            </template>
          </el-table-column>
          <el-table-column label="吨" prop="ton" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.ton"
                placeholder="请输入吨"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="生产时长" prop="scsc" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.scsc"
                placeholder="请输入生产时长"
                :disabled="openDisabled"
                @change="automatic1(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column label="产线工时" prop="cxgs" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.cxgs"
                placeholder="请输入产线工时"
                :disabled="openDisabled"
                @change="automatic2(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column label="生产线人数" prop="scxrs" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.scxrs"
                placeholder="请输入生产线人数"
                :disabled="openDisabled"
                @change="automatic2(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column label="生产效率" prop="scxl" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.scxl"
                placeholder="请输入生产效率"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="产线人均小时产量"
            prop="cxrjcl"
            minWidth="110px"
          >
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.cxrjcl"
                placeholder="请输入产线人均小时产量"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="瓶坯_实用" prop="ppSy" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.ppSy"
                placeholder="请输入瓶坯_实用"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column label="瓶坯_消耗率" prop="ppXh" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.ppXh"
                placeholder="请输入瓶坯_消耗率"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="瓶盖_实用" prop="pgSy" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.pgSy"
                placeholder="请输入瓶盖_实用"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column label="瓶盖_消耗率" prop="pgXh" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.pgXh"
                placeholder="请输入瓶盖_消耗率"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="商标_实用" prop="sbSy" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.sbSy"
                placeholder="请输入商标_实用"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column label="商标_消耗率" prop="sbXh" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.sbXh"
                placeholder="请输入商标_消耗率"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="纸箱_实用" prop="zxSy" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.zxSy"
                placeholder="请输入纸箱_实用"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column label="纸箱_消耗率" prop="zxXh" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.zxXh"
                placeholder="请输入纸箱_消耗率"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="水_实用" prop="waterSy" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.waterSy"
                placeholder="请输入水_实用"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column label="水_吨用量" prop="waterYl" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.waterYl"
                placeholder="请输入水_吨用量"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="电_度" prop="electricSy" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.electricSy"
                placeholder="请输入电_度"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column label="电_吨用量" prop="electricYl" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.electricYl"
                placeholder="请输入电_吨用量"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="气_度" prop="airSy" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.airSy"
                placeholder="请输入气_度"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column label="气_吨用量" prop="airYl" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.airYl"
                placeholder="请输入气_吨用量"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="燃气_实用" prop="gasSy" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.gasSy"
                placeholder="请输入燃气_实用"
                :disabled="openDisabled"
                @change="automatic3(scope)"
              />
            </template>
          </el-table-column>
          <el-table-column label="燃气_吨用量" prop="gasYl" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.gasYl"
                placeholder="请输入燃气_吨用量"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="标煤能耗_吨新水"
            prop="coalSy"
            minWidth="140px"
          >
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.coalSy"
                placeholder="请输入标煤能耗"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="标煤能耗_吨电力"
            prop="coalYl"
            minWidth="140px"
          >
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.coalYl"
                placeholder="请输入标煤能耗"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="水利用率" prop="water" minWidth="110px">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.water"
                placeholder="请输入水利用率"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <!-- <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column> -->
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAnalysisProductMonth,
  getAnalysisProductMonth,
  oneKey,
  delAnalysisProductMonth,
  addAnalysisProductMonth,
  updateAnalysisProductMonth,
} from "@/api/report/analysis/analysisProductMonth";
import { listProductInfo } from "@/api/basic/productInfo/productInfo";

export default {
  name: "AnalysisProductMonth",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedRptAnalysisProductMonthSub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仰口生产报每月表格数据
      analysisProductMonthList: [],
      // 仰口生产报每月子表格数据
      rptAnalysisProductMonthSubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        scny: null,
        scny1: null,
        ksrq: null,
        jsrq: null,
        bbmc: null,
        bbbh: null,
        jd: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      scny: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //修改箱数其它更新
    updateAll(scope, event) {
      let mlval = scope.row.ggname;
      // console.log(mlval.indexOf('×'));
      mlval = mlval.substring(0, mlval.indexOf("×"));
      // console.log(mlval);

      // console.log(Number(scope.row.capacity ),scope.row.ggname.lastIndexOf(""));
      /**吨 */
      scope.row.ton = (
        ((Number(scope.row.capacity) / 1000) * Number(event) * 24) /
        1000
      ).toFixed(2);
      /* 生产效率 */
      // console.log(scope.row);

      let sc = scope.row.scsc;
      if (sc != null && sc != "" && sc != 0) {
        scope.row.scxl = (Number(event) / 750 / sc).toFixed(2);
      } else {
        scope.row.scxl = 0;
      }
      //瓶胚消耗

      scope.row.ppXh = (
        (Number(scope.row.pgSy) -
          Number(event) * Number(scope.row.dateNumber)) /
        (Number(event) * Number(scope.row.dateNumber))
      ).toFixed(4);
      //瓶盖消耗
      scope.row.pgXh = (
        (Number(scope.row.pgSy) -
          Number(event) * Number(scope.row.dateNumber)) /
        (Number(event) * Number(scope.row.dateNumber))
      ).toFixed(4);
      //商标消耗
      scope.row.sbXh = (
        (Number(scope.row.sbSy) -
          Number(event) * Number(scope.row.dateNumber)) /
        (Number(event) * Number(scope.row.dateNumber))
      ).toFixed(4);
      //纸箱实用
      scope.row.zxSy = event;

      //水用量
      scope.row.waterYl = (
        Number(scope.row.waterSy) / Number(scope.row.ton)
      ).toFixed(2);
      //电用量
      scope.row.electricYl = (
        Number(scope.row.electricSy) / Number(scope.row.ton)
      ).toFixed(2);
      //气用量
      scope.row.airYl = (
        Number(scope.row.airSy) / Number(scope.row.ton)
      ).toFixed(2);
      /* 燃气 */
      scope.row.coalSy = (
        ((Number(scope.row.waterSy) - Number(scope.row.ton)) * 0.0857) /
        Number(scope.row.ton)
      ).toFixed(2);
      /*吨 电力*/
      scope.row.coalYl = (
        (Number(scope.row.electricSy) * 0.1229) /
        Number(scope.row.ton)
      ).toFixed(2);

      if (scope.row.waterSy != 0 && scope.row.waterSy != null) {
        scope.row.water = (
          Number(scope.row.ton) / Number(scope.row.waterSy)
        ).toFixed(2);
      } else {
        scope.row.water = 0;
      }
    },
    automatic1(scope) {
      //生产效率联动
      let scsc_temp = this.rptAnalysisProductMonthSubList[scope.$index].scsc;
      let ton_temp = this.rptAnalysisProductMonthSubList[scope.$index].ton;

      if (scsc_temp != null && scsc_temp != "" && scsc_temp != 0) {
        this.rptAnalysisProductMonthSubList[scope.$index].scxl = (
          Number(this.rptAnalysisProductMonthSubList[scope.$index].xb) /
          750 /
          scsc_temp
        ).toFixed(2);
      } else {
        this.rptAnalysisProductMonthSubList[scope.$index].scxl = 0;
      }
    },
    automatic2(scope) {
      let a = this.rptAnalysisProductMonthSubList[scope.$index].cxgs;
      let b = this.rptAnalysisProductMonthSubList[scope.$index].scxrs;
      if (a != null && a != "" && b != "" && b != null) {
        this.rptAnalysisProductMonthSubList[scope.$index].cxrjcl = (
          this.rptAnalysisProductMonthSubList[scope.$index].xb /
          (a * b)
        ).toFixed(2);
      }
    },
    automatic3(scope) {
      let a = this.rptAnalysisProductMonthSubList[scope.$index].zxSy;
      if (a != null && a != "") {
        this.rptAnalysisProductMonthSubList[scope.$index].zxXh = (
          (scope.row.zxSy - scope.row.xb) /
          scope.row.xb
        ).toFixed(2);
      }
      //瓶胚消耗
      this.rptAnalysisProductMonthSubList[scope.$index].ppXh = (
        (Number(scope.row.ppSy) - Number(scope.row.xb) * 24) /
        (Number(scope.row.xb) * 24)
      ).toFixed(4);

      //瓶盖消耗
      this.rptAnalysisProductMonthSubList[scope.$index].pgXh = (
        (Number(scope.row.pgSy) - Number(scope.row.xb) * 24) /
        (Number(scope.row.xb) * 24)
      ).toFixed(4);

      //商标消耗
      this.rptAnalysisProductMonthSubList[scope.$index].sbXh = (
        (Number(scope.row.sbSy) - Number(scope.row.xb) * 24) /
        (Number(scope.row.xb) * 24)
      ).toFixed(4);

      // let timeNum=0;
      // this.rptAnalysisProductSubList.forEach((item) => {
      //   // console.log(item);
      //     timeNum += parseFloat(item.scsc);
      //   });

      //水
      this.rptAnalysisProductMonthSubList[scope.$index].waterYl = (
        Number(scope.row.waterSy) / Number(scope.row.ton)
      ).toFixed(2);
      // 电
      this.rptAnalysisProductMonthSubList[scope.$index].electricYl = (
        Number(scope.row.electricSy) / Number(scope.row.ton)
      ).toFixed(2);
      // 气
      this.rptAnalysisProductMonthSubList[scope.$index].airYl = (
        Number(scope.row.airSy) / Number(scope.row.ton)
      ).toFixed(2);

      //燃气
      let ton = scope.row.ton;
      if (ton != "" && ton != null) {
        this.rptAnalysisProductMonthSubList[scope.$index].gasYl = (
          scope.row.gasSy / ton
        ).toFixed(2);
      }

      /* 标准 吨水 */
      this.rptAnalysisProductMonthSubList[scope.$index].coalSy = (
        ((Number(scope.row.waterSy) - Number(scope.row.ton)) * 0.0857) /
        Number(scope.row.ton)
      ).toFixed(2);
      /*标准煤吨 电力*/
      this.rptAnalysisProductMonthSubList[scope.$index].coalYl = (
        (Number(scope.row.electricSy) * 0.1229) /
        Number(scope.row.ton)
      ).toFixed(2);
    },
    correlation() {
      this.form.jsrq = this.form.scny + "-26";
      let d = new Date(this.form.scny);
      let month = d.getMonth();
      if (month < 10) {
        month = "0" + month;
      }
      if (month == 12) {
        month = "01";
      }
      this.form.ksrq = d.getFullYear() + "-" + month + "-27";
      this.scny = this.form.scny;
    },
    oneKey() {
      oneKey({ ksrq: this.form.ksrq, jsrq: this.form.jsrq }).then(
        (response) => {
          // console.log(111111, response);
          if (response.data == null) {
            this.msgError("该时间段内没有记录");
            return null;
          }
          // console.log(response.data);
          var data = response.data;
          var subList = data.rptAnalysisProductSubList;

          //表单重置
          this.reset();
          //给主表赋值
          this.form.scny = this.scny;
          this.form.ksrq = data.ksrq;
          this.form.jsrq = data.jsrq;
          this.form.bbmc = "仰口生产月报表";
          this.form.jd = data.jd;

          // 添加子表
          this.addSub(subList);
          this.openDisabled = false;
          this.open = true;
          this.title = "添加仰口生产报每月";
        }
      );
    },
    /** 添加子表  */
    addSub(subList) {
      /* 计算总时长 */
      var timeNum = 0;
      subList.forEach((item) => {
        timeNum += parseFloat(item.scsc);
      });

      subList.forEach((item) => {
        // listProductInfo({ name: item.ggname }).then((resp) => {
        // console.log(item);
        //重置
        var obj = new Object();
        obj.gg = item.gg; //规格
        obj.ggname = item.ggname; //品名
        obj.xb = Number(item.xb).toFixed(0); //箱包
        obj.ton = "";
        obj.scsc = Number(item.scsc).toFixed(2); //生产时长
        obj.cxgs = Number(item.cxgs).toFixed(2); //产线工时
        obj.scxrs = Number(item.scxrs).toFixed(0); //生产线人数
        obj.scxl = "";
        obj.cxrjcl = "";
        obj.ppSy = Number(item.ppSy).toFixed(0); //瓶胚
        obj.ppXh = "";
        obj.pgSy = Number(item.pgSy).toFixed(0); //瓶盖
        obj.pgXh = "";
        obj.sbSy = Number(item.sbSy).toFixed(0); //商标
        obj.sbXh = "";
        obj.zxSy = Number(item.zxSy).toFixed(0); //纸箱
        obj.zxXh = "";
        obj.waterSy = Number(item.waterSy).toFixed(1); //水
        obj.waterYl = "";
        obj.electricSy = Number(item.electricSy).toFixed(1); //电
        obj.electricYl = "";
        obj.airSy = Number(item.airSy).toFixed(1); //气
        obj.airYl = "";
        obj.gasSy = Number(item.gasSy).toFixed(1); //燃气
        obj.gasYl = "";
        obj.coalSy = "";
        obj.coalYl = "";
        obj.water = "";
        obj.remark = "";

        obj.capacity = item.capacity;
        obj.dateNumber = item.dateNumber;
        //局部变量
        let xbls = Number(obj.xb);
        /* 给吨 赋值 */
        let tonls = (Number(item.capacity) * 24 * xbls) / 1000000;
        obj.ton = tonls.toFixed(2);
        /* 生产效率 */
        if (item.scsc != null && item.scsc != "") {
          obj.scxl = (xbls / 750 / item.scsc).toFixed(2);
        }
        /* 人均小时产量 */
        if (
          item.cxgs != null &&
          item.cxgs != "" &&
          item.scxrs != "" &&
          item.scxrs != null
        ) {
          obj.cxrjcl = (xbls / (item.cxgs * item.scxrs)).toFixed(2);
        }

        /* 原辅料消耗率 */
        if (xbls != 0) {
          // console.log(xbls);
          obj.ppXh = (
            (obj.ppSy - xbls * item.dateNumber) /
            (xbls * item.dateNumber)
          ).toFixed(4);
          // console.log("-1-");
          // console.log(obj.ppSy);
          // console.log("-2-");
          // console.log(xbls * item.dateNumber);

          obj.pgXh = (
            (obj.pgSy - xbls * item.dateNumber) /
            (xbls * item.dateNumber)
          ).toFixed(4);

          obj.sbXh = (
            (obj.sbSy - xbls * item.dateNumber) /
            (xbls * item.dateNumber)
          ).toFixed(4);

          obj.zxXh = ((obj.zxSy - xbls) / xbls).toFixed(4);
        }

        /* 能源消耗率 */
        //obj.waterSy = item.ssy;
        // obj.electricSy = item.dsy;
        // obj.airSy = item.qsy;
        if (tonls != 0) {
          obj.waterYl = (obj.waterSy / tonls).toFixed(2);
          obj.electricYl = (obj.electricSy / tonls).toFixed(2);
          obj.airYl = (Number(item.airSy) / tonls).toFixed(2);
          /* 燃气 */
          obj.gasYl = (Number(item.gasSy) / tonls).toFixed(2);

          obj.coalSy = (((obj.waterSy - tonls) * 0.0857) / tonls).toFixed(2);
          obj.coalYl = ((obj.electricSy * 0.1229) / tonls).toFixed(2);
        }
        if (item.waterSy != "" && item.waterSy != null) {
          obj.water = (tonls / obj.waterSy).toFixed(2);
        }
        this.rptAnalysisProductMonthSubList.push(obj);
        // });
      });
    },
    /** 查询仰口生产报每月列表 */
    getList() {
      this.loading = true;
      listAnalysisProductMonth(this.queryParams).then((response) => {
        this.analysisProductMonthList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        scny: null,
        ksrq: null,
        jsrq: null,
        bbmc: null,
        bbbh: "LK-QF-LS1-10-007-11-01",
        jd: "仰口生产基地",
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.rptAnalysisProductMonthSubList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.scny != null) {
        let arr = this.queryParams.scny;
        this.queryParams.scny = arr[0];
        this.queryParams.scny1 = arr[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加仰口生产报每月";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getAnalysisProductMonth(id).then((response) => {
        this.form = response.data;
        this.rptAnalysisProductMonthSubList =
          response.data.rptAnalysisProductMonthSubList;
        this.open = true;
        this.title = "修改仰口生产报每月";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getAnalysisProductMonth(id).then((response) => {
        this.form = response.data;
        this.rptAnalysisProductMonthSubList =
          response.data.rptAnalysisProductMonthSubList;
        this.open = true;
        this.title = "查看仰口生产报每月";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.rptAnalysisProductMonthSubList =
            this.rptAnalysisProductMonthSubList;
          if (this.form.id != null) {
            updateAnalysisProductMonth(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const rLoading = this.openLoading();
            addAnalysisProductMonth(this.form)
              .then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              })
              .catch((error) => {
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除仰口生产报每月编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delAnalysisProductMonth(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 仰口生产报每月子序号 */
    rowRptAnalysisProductMonthSubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 仰口生产报每月子添加按钮操作 */
    handleAddRptAnalysisProductMonthSub() {
      let obj = {};
      obj.gg = "";
      obj.ggname = "";
      obj.xb = "";
      obj.ton = "";
      obj.scsc = "";
      obj.cxgs = "";
      obj.scxrs = "";
      obj.scxl = "";
      obj.cxrjcl = "";
      obj.ppSy = "";
      obj.ppXh = "";
      obj.pgSy = "";
      obj.pgXh = "";
      obj.sbSy = "";
      obj.sbXh = "";
      obj.zxSy = "";
      obj.zxXh = "";
      obj.waterSy = "";
      obj.waterYl = "";
      obj.electricSy = "";
      obj.electricYl = "";
      obj.airSy = "";
      obj.airYl = "";
      obj.gasSy = "";
      obj.gasYl = "";
      obj.coalSy = "";
      obj.coalYl = "";
      obj.water = "";
      obj.remark = "";
      this.rptAnalysisProductMonthSubList.push(obj);
    },
    /** 仰口生产报每月子删除按钮操作 */
    handleDeleteRptAnalysisProductMonthSub() {
      if (this.checkedRptAnalysisProductMonthSub.length == 0) {
        this.$alert("请先选择要删除的仰口生产报每月子数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.rptAnalysisProductMonthSubList.splice(
          this.checkedRptAnalysisProductMonthSub[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleRptAnalysisProductMonthSubSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.rptAnalysisProductMonthSub.clearSelection();
        this.$refs.rptAnalysisProductMonthSub.toggleRowSelection(
          selection.pop()
        );
      } else {
        this.checkedRptAnalysisProductMonthSub = selection;
      }
    },
    excelhandleView(row) {
      this.download(
        "productionProcess/analysisProductMonth/exportexcel?id=" + row.id,
        {
          ...this.queryParams,
        },
        "仰口生产产出月报表" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "productionProcess/analysisProductMonth/export",
        {
          ...this.queryParams,
        },
        "仰口生产报每月" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    //接收编号
    nameChange(e) {
      this.form.bbbh = e;
    },
  },
};
</script>
